Function.RegisterNamespace("System.IO.FileStrategy");

System.IO.FileStrategy.Rhino=function(){
    // Private Members
    var _unicodeRegex=/[^\u0000-\u00ff]/;

    // IFileStrategy Members
    this.DeleteFile=function(path){
        var file=new java.io.File(System.IO.Path.Normalize(path));
        file["delete"]();
    };
    
    this.Exists=function(path){
        if(!!path){
            var file=new java.io.File(System.IO.Path.Normalize(path));
            return file.exists()&&file.isFile();
        }
        return false;
    };

    this.GetFile=function(path){
        if(this.Exists(path)){
            return readFile(System.IO.Path.Normalize(path),"UTF-8")+'';
        }
        return null;
    };
        
    this.SaveFile=function(path,text){
        var fileWriter=null;
        try {
            if(_unicodeRegex.test(text)){
                fileWriter=new java.io.BufferedWriter(new java.io.OutputStreamWriter(new java.io.FileOutputStream(System.IO.Path.Normalize(path)), "UTF-8"));
            }else{
                fileWriter=new java.io.FileWriter(new java.io.File(System.IO.Path.Normalize(path)));
            }
            fileWriter.write(text);
        }catch(e) {
            throw e;
        }finally{
            if(fileWriter!=null)fileWriter.close();
        }
    };

    // IStrategySpecification Members
    this.IsSatisfiedBy=function(candidate){
        return (typeof(java)!='undefined' && typeof(environment)!='undefined' && typeof(defineClass)=="function" && typeof(loadClass)=="function");
    };

};

System.IO.FileStrategy.Rhino.Implement(System.IO.FileStrategy.IFileStrategy,'System.IO.FileStrategy.Rhino');
System.IO.FileStrategy.Rhino.Implement(System.Script.Strategy.IStrategySpecification,'System.IO.FileStrategy.Rhino');

System.IO.File.Strategies.Add(System.IO.FileStrategy.Rhino);